package com.example.active.sf;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.broker.region.policy.RedeliveryPolicyMap;
import org.apache.activemq.command.ActiveMQDestination;

import javax.jms.*;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/17 8:23
 */
public class Consumer
{
    public static final String ACTIVEMQ_URL = "nio://192.168.0.118:61618";
//    public static final String ACTIVEMQ_URL = "failover:(tcp://192.168.0.118:61616," +
//            "tcp://192.168.0.118:61617," +
//            "tcp://192.168.0.118:61618)?randomize=false";

    static ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

    public static void main(String[] args) throws JMSException
    {
        ActiveMQConnection connection = (ActiveMQConnection)factory.createConnection();

        System.out.println("消费者1");
        /*系统默认重发六次，现在用代码改成三次*/
        RedeliveryPolicy topicPolicy = new RedeliveryPolicy();
//        topicPolicy.setInitialRedeliveryDelay(0);
//        topicPolicy.setRedeliveryDelay(1000);
//        topicPolicy.setUseExponentialBackOff(false);
        topicPolicy.setMaximumRedeliveries(3);

        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        try
        {
            //创建目的地
            Queue queue = session.createQueue("queue01");
            RedeliveryPolicyMap map = connection.getRedeliveryPolicyMap();
            map.put((ActiveMQDestination) queue, topicPolicy);
            //创建消费者
            MessageConsumer consumer = session.createConsumer(queue);
            connection.start();
            for (; ; )
            {
                //生产者产生的消息就是TextMessage
                TextMessage receive = (TextMessage) consumer.receive(1000);
                if (null != receive)
                {
//                    receive.acknowledge();
                    System.out.println("接收消息：" + receive.getText());
                } else
                {
                    break;
                }
            }

            //设置监听器
/*            consumer.setMessageListener(message -> {
//                if(message instanceof TextMessage &&null !=message){
                    try
                    {
                        TextMessage textMessage = ((TextMessage) message);
                        System.out.println("接收消息："+textMessage.getText());
                        session.rollback();
//                        System.out.println("接收消息属性："+textMessage.getStringProperty("01"));
                    } catch (JMSException e)
                    {
                        e.printStackTrace();
                    }
//                }
                *//*if(message instanceof MapMessage &&null !=message){
                    try
                    {
                        MapMessage mapMessage = ((MapMessage) message);
                        System.out.println("接收消息："+mapMessage.getString("v1"));
                    } catch (JMSException e)
                    {
                        e.printStackTrace();
                    }
                }*//*
            });
            System.in.read();//保证控制台不灭*/
            consumer.close();
//            session.commit();

            session.close();
            connection.close();
        } catch (JMSException e)
        {
            e.printStackTrace();
        }
    }
}
